<template>
    <div>
        <el-form ref="loginForm" :model="form" :rules="rules" label-width="80px" class="login-box">
            <h3 class="login-title">欢迎注册</h3>
            <el-form-item label="姓名" prop="name">
                <el-input type="text" placeholder="请输入您的姓名" v-model="form.name"/>
            </el-form-item>
            <el-form-item label="电话" prop="phone">
                <el-input type="text" placeholder="请输入您的电话" v-model="form.phone"/>
            </el-form-item>
            <el-form-item label="所属分组" prop="phone">
                <el-input type="text" placeholder="请输入所属分组" v-model="form.orgId"/>
            </el-form-item>
            <el-form-item label="性别" prop="sex">
                <el-select v-model="form.sex" placeholder="请选择性别">
                    <el-option label="男" value="男"></el-option>
                    <el-option label="女" value="女"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" placeholder="请输入密码" v-model="form.password" auto-complete="off"/>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
                <el-input type="password" v-model="form.checkPass" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button v-on:click="toLogin()">登录</el-button>
                <el-button type="primary" v-on:click="onSubmit('loginForm')">注册</el-button>
            </el-form-item>
        </el-form>

    </div>
</template>

<script>
    export default {
        name: "Login",
        data() {
            let validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.form.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                form: {
                    name: '',
                    phone: '',
                    sex: '',
                    orgId: '',
                    password: '',
                    checkPass: '',
                },
                // 表单验证，需要在 el-form-item 元素中增加 prop 属性
                rules: {
                    name: [
                        {required: true, message: '姓名不可为空', trigger: 'blur'}
                    ],
                    phone: [
                        {required: true, message: '电话不可为空', trigger: 'blur'}
                    ],
                    sex: [
                        {required: true, message: '请选择性别', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '密码不可为空', trigger: 'blur'}
                    ],
                    checkPass: [
                        {validator: validatePass2, trigger: 'blur'}
                    ]
                },

                // 对话框显示和隐藏
                dialogVisible: false
            }
        },
        methods: {
            toLogin() {
                this.$router.push({path: '/login'})
            },
            onSubmit(formName) {
                // 为表单绑定验证功能
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$request(this.$api.register, this.form, (data) => {
                            console.info(data);
                            this.$message({
                                message: '注册成功',
                                type: 'info'
                            });
                            this.$router.push({path: '/login'});
                        });
                    } else {
                        return false;
                    }
                });
            }
        }
    }
</script>

<style lang="less" scoped>
    .login-box {
        border: 1px solid #DCDFE6;
        width: 350px;
        margin: 180px auto;
        padding: 35px 35px 15px 35px;
        border-radius: 5px;
        -webkit-border-radius: 5px;
        -moz-border-radius: 5px;
        box-shadow: 0 0 25px #909399;
    }

    .login-title {
        text-align: center;
        margin: 0 auto 40px auto;
        color: #303133;
    }
</style>